package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author ghy
 * @version v1.0
 * create on 2022/11/18 10:14
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.warn("insertFill {}", metaObject);
        //维护插入时公共字段的值
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        //从session中拿，现在先给假值
        //在这里，从ThreadLocal中获取值
        log.error("当前线程ID:{}", Thread.currentThread().getId());
        metaObject.setValue("createUser", ThreadLocalUtils.get());
        metaObject.setValue("updateUser", ThreadLocalUtils.get());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.warn("updateFill {}", metaObject);
        log.error("当前线程ID:{}", Thread.currentThread().getId());
        //维护更新时公共字段的值
        metaObject.setValue("updateTime", LocalDateTime.now());
        //从session中拿，现在先给假值
        metaObject.setValue("updateUser", ThreadLocalUtils.get());
    }

}
